14 Lecture
CS201
Midterm & Final Term Short Notes
Pointers
Pointers are variables that hold the memory addresses of other variables. They allow for dynamic memory allocation and manipulation of data structures. Pointers are commonly used in programming to pass arguments to functions, to allocate and dea
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
- What is a pointer in C++? a) A keyword used to declare variables b) A variable that stores the memory address of another variable c) A data type used to store text d) A type of loop used in programming
Answer: b) A variable that stores the memory address of another variable
- Which operator is used to declare a pointer variable in C++? a) * b) & c) $ d) #
Answer: a) *
- Which of the following statements is true about null pointers in C++? a) They point to the address 0x0000. b) They point to the address 0xFFFF. c) They are not allowed in C++. d) They can be used to indicate that a pointer does not point to anything.
Answer: d) They can be used to indicate that a pointer does not point to anything.
- What is the difference between a pointer and a reference in C++? a) A pointer is a variable that holds a memory address, while a reference is an alias for another variable. b) A reference is a variable that holds a memory address, while a pointer is an alias for another variable. c) There is no difference between a pointer and a reference. d) A pointer is used for dynamic memory allocation, while a reference is used for static memory allocation.
Answer: a) A pointer is a variable that holds a memory address, while a reference is an alias for another variable.
- What is the use of the
new
operator in C++? a) To declare a new variable b) To delete a variable c) To allocate memory for a new object d) To free memory for an existing object
Answer: c) To allocate memory for a new object
- What is the use of the
delete
operator in C++? a) To declare a new variable b) To delete a variable c) To allocate memory for a new object d) To free memory for an existing object
Answer: d) To free memory for an existing object
- What is a dangling pointer? a) A pointer that points to an invalid memory address b) A pointer that points to a valid memory address c) A pointer that points to a null value d) A pointer that points to an uninitialized value
Answer: a) A pointer that points to an invalid memory address
- Which of the following is the correct syntax to declare a pointer to a constant integer in C++? a) const int *ptr; b) int const *ptr; c) const int * const ptr; d) int * const ptr;
Answer: b) int const *ptr;
- What is a void pointer in C++? a) A pointer that points to a function b) A pointer that points to a structure c) A pointer that has no data type associated with it d) A pointer that points to an integer value
Answer: c) A pointer that has no data type associated with it
- What is the use of the
reinterpret_cast
operator in C++? a) To cast a pointer from one data type to another b) To cast a reference from one data type to another c) To cast a variable from one data type to another d) To cast a pointer from one function to another
Answer: a) To cast a pointer from one data type to another
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is a pointer in C programming? A pointer is a variable that stores a memory address. It allows programmers to directly manipulate memory and is useful for efficient dynamic memory allocation.
How do you declare a pointer variable in C? A pointer variable is declared by adding an asterisk (*) before the variable name, for example: int *ptr;
What is the purpose of the ampersand (&) operator in C? The ampersand (&) operator is used to get the address of a variable in memory, for example: #
How do you access the value pointed to by a pointer in C? You can access the value pointed to by a pointer by using the dereference operator (*) before the pointer variable, for example: *ptr;
What is a null pointer in C? A null pointer is a pointer that does not point to any valid memory location. It is represented in C by the value 0 or NULL.
How do you use pointers to dynamically allocate memory in C? You can use the malloc() function to dynamically allocate memory in C, and then use a pointer to access the allocated memory, for example: int ptr = (int) malloc(sizeof(int));
How do you pass pointers as function arguments in C? You can pass pointers as function arguments by declaring the function parameter as a pointer and then passing the memory address of the variable as an argument, for example: void myFunction(int *ptr);
What is a pointer arithmetic in C? Pointer arithmetic in C involves manipulating the memory address stored in a pointer variable using arithmetic operations, such as addition or subtraction.
What is a void pointer in C? A void pointer is a special type of pointer that can point to any type of data. It is useful for generic programming and dynamic memory allocation.
How do you use pointers to manipulate arrays in C? You can use pointers to manipulate arrays in C by using pointer arithmetic to access array elements, for example: int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; printf("%d", *(ptr + 2)); // prints 3
int *ptr;
ptr
is a pointer to an integer variable, you can access the value of that variable like this:
int num = 5;
int *ptr = #
printf("%d", *ptr); // prints 5
malloc()
function is used to allocate memory dynamically, and returns a pointer to the allocated memory.
For example, to allocate memory for an integer variable, you would write:
int *ptr = (int*) malloc(sizeof(int));
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("%d", *(ptr + 2)); // prints 3
void myFunction(int *ptr);